<?php
defined('BASEPATH') OR exit('No direct script access allowed');
// +----------------------------------------------------------------------
// | 用户中心模型
// +----------------------------------------------------------------------
// | Copyright (c) 2015
// +----------------------------------------------------------------------
// | Author: baiping 125618036@qq.com http://www.webipcode.com
// +----------------------------------------------------------------------
// | 2015-7-10下午5:44:10
// +----------------------------------------------------------------------

class Member_model extends CI_Model{
	
	private $table_name;
	public function __construct(){
		parent::__construct();
		$this->table_name = 'member';
	}
	
	/**
	 * 获取数据
	 * @param array $where 获取条件
	 * @param string $field 获取字段
	 * @param string $limit 获取数量
	 * @param string $order 排序
	 * @author Baip
	 * @deprecated:limit的传值为（每页记录数,偏移数）生成的sql语句为 limit(偏移数,记录数)
	 */
	public function getData($where=array(), $field="uid,username,email,last_login_time,last_login_ip,status", $limit=15, $offset=0,  $order="uid DESC"){
		$res = $this->db->select($field)->where($where)->order_by($order)->limit($limit, $offset)->get($this->table_name);
		return $res->result();
	}
	
	/**
	 * 获取数据部数
	 * @param unknown_type $where
	 * @param unknown_type $field
	 */
	public function getCount($where=array(), $field="uid"){
		$count =  $this->db->where($where)->select($field)->count_all_results($this->table_name);
		return $count;
	}
	
	/**
	 * 添加数据
	 */
	public function addData(){
		$username  = $this->input->post('username',true);//用户名
		$password  = $this->input->post('password',true);//密码
		$email  = intval( $this->input->post('email',true) );//邮箱
	
		//产生随机字符串
		$this->load->helper('string');//载入字符串辅助函数
		$salt = strtolower( random_string('alnum', 6) );//产生随机码
		$data_arr['username'] = $username;
		$data_arr['password'] = encryption_pass($password,$salt);//将密码进行两次加密，是加密密码和随机码组合再次加密
		$data_arr['salt'] = $salt;
		$data_arr['email'] = $email;
		$data_arr['reg_time'] = time();//注册时间
		$data_arr['reg_ip'] = ip2long( $this->input->ip_address() );//将客户的ip地址转为整形
		$data_arr['status'] = 1;//用户状态
		
		$query = $this->db->insert($this->table_name, $data_arr);
		return $this->db->insert_id();
	}
	
	/**
	 * 更新数据
	 * @param string/array $map_value 操作条件值
	 * @param int $type 操作类型 1：对等，2：in_where...
	 * @param string $map_field : 操作条件字段
	 * @param array $data_arr 更新的数据
	 */
	public function updateData($map_value, $type="", $map_field="uid",$data_arr){
		switch ($type){
			case 1:
				$this->db->where(array($map_field,$map_value));
				break;
			case 2:
				$this->db->where_in($map_field, $map_value);
				break;
		}
		
		$query = $this->db->update($this->table_name, $data_arr);
		return $query;
	}
	
	/**
	 * 修改用户密码
	 * @param int $uid 用户uid
	 * @author Baip
	 */
	public function updatePwd($uid){
		$password = $this->input->post('password', true);
		//产生随机字符串
		$this->load->helper('string');//载入字符串辅助函数
		$salt = strtolower( random_string('alnum', 6) );//产生随机码
		$data_arr['password'] = encryption_pass($password,$salt);//将密码进行两次加密，是加密密码和随机码组合再次加密
		$data_arr['salt'] = $salt;
		$query = $this->db->where('uid', $uid)->update($this->table_name, $data_arr);
		return $query;
	}
	
	/**
	 * 删除数据
	 * @param unknown_type $map_value
	 * @param unknown_type $type
	 * @param unknown_type $map_field
	 */
	public function deleteData($map_value, $type="", $map_field="uid"){
		switch ($type){
			case 1:
				$this->db->where(array($map_field,$map_value));
				break;
			case 2:
				$this->db->where_in($map_field, $map_value);
				break;
		}
		
		$query = $this->db->delete($this->table_name);
		return $query;
	}
	
	/**
	 * 获取字段值 然后根据传递的字段返回的数据为（前字段的值为下标后字段的值为值）
	 * @param array $where 读取条件
	 * @param string $field 读取字段
	 * @param booler $type
	 */
	public function getField($where, $field="uid,username,email,salt", $type = true){
		$res = $this->db->select($field)->where($where)->get( $this->table_name );
		$query = $res->result_array();
		$field = explode(",", $field);
		if ( $type ){
			$query = count( $field ) > 1 ? i_array_column($query, $field[1], $field[0]) : i_array_column($query, $field[0]);
		}
		return $query;
	}
	/**
	 * 获取单条数据
	 * @param array $where 读取条件
	 * @param string $field 读取字段
	 */
	public function getFind($where, $field="uid,username,password,email,salt,status", $limit=1){
		$res = $this->db->select($field)->where($where)->limit($limit)->get( $this->table_name );
		$query = $res->row_array();
		return $query;
	}
	
	/**
	 * 用户登录
	 * @param string $username 用户名
	 * @param string $password 密码
	 */
	public function login($username, $password){
		$map = array('username'=> $username);
		$query = $this->getFind($map);
		if ( !is_null($query) ) {
			//判断用户是否锁定或删除
			if ( 1 != intval($query['status'])   ){
				$code = -$query['status'];
				return $code;//2:锁定3:删除
			}
			//将用户名读取到的密码随机值和用户添加的密码与数据库中的密码做比较
			$new_password = encryption_pass($password,$query['salt']) ;
			
			if ( $new_password != $query['password'] ){
				return -10;//密码错误
			}else{
				/* 更新登录信息 */
				$data = array(
						'last_login_time' => time(),
						'last_login_ip'   => ip2long( $this->input->ip_address() ),
				);
				$this->db->set('login_num', "login_num + 1", false);
				$this->db->where('uid', $query['uid'])->update($this->table_name, $data);
				
				/* 记录登录SESSION和COOKIES */
				$auth = array(
						'uid'     => $query['uid'],
						'username'=> $query['username'],
						'email'   => $query['email'],
				);
				
				$this->session->set_userdata('user_auth', $auth);
				$this->session->set_userdata('user_auth_sign', data_auth_sign($auth));
				return true;
			}
		}else{
			return -5;//没有此帐号
		}
	}
	
	/**
	 * 用户退出管理系统
	 */
	public function logout(){
		$this->session->unset_userdata('user_auth_sign');
		$this->session->unset_userdata('user_auth');
	}
	
}